package com.backend.securitiessystem.aop;

import com.backend.securitiessystem.enums.AppHttpCodeEnum;
import com.backend.securitiessystem.exception.BaseException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 爬虫 aop 增强
 *
 * @author qksuki
 * @date 2024-05-21 11:01:18
 */
@Component
@Aspect
public class CrawlerAspect {

    /**
     * 检查爬取的数据是否为空
     *
     * @param joinPoint 连接点
     * @return {@code Object }
     * @throws Throwable throwable
     * @author qksuki
     * @date 2024-05-21 11:26:05
     */
    @Around("execution(* com.backend.securitiessystem.crawler.service.impl.CrawlerProviderImpl.get*(..))")
    public Object checkListEmpty(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = joinPoint.proceed();

        if (result instanceof List) {
            List<?> list = (List<?>) result;
            if (list.isEmpty()) {
                throw new BaseException(AppHttpCodeEnum.CRAWLED_DATE_IS_EMPTY);
            }
        }

        return result;
    }
}

